package com.cp.admin.modular.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cp.admin.api.entity.Result;
import com.cp.admin.core.common.exception.BizExceptionEnum;
import com.cp.admin.modular.system.dao.DictMapper;
import com.cp.admin.modular.system.model.Dict;
import com.cp.admin.modular.system.service.IDictService;
import com.cp.core.exception.GunsException;
import com.cp.admin.core.common.constant.factory.MutiStrFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements IDictService {

    @Resource
    private DictMapper dictMapper;

    @Override
    public void addDict(String dictName, String dictValues) {
        //判断有没有该字典
        List<Dict> dicts = dictMapper.selectList(new EntityWrapper<Dict>().eq("name", dictName).and().eq("pid", 0));
        if (dicts != null && dicts.size() > 0) {
            throw new GunsException(BizExceptionEnum.DICT_EXISTED);
        }

        //解析dictValues
        List<Map<String, String>> items = MutiStrFactory.parseKeyValue(dictValues);

        //添加字典
        Dict dict = new Dict();
        dict.setName(dictName);
        dict.setNum(0);
        dict.setPid(0);
        this.dictMapper.insert(dict);

        //添加字典条目
        for (Map<String, String> item : items) {
            String num = item.get(MutiStrFactory.MUTI_STR_KEY);
            String name = item.get(MutiStrFactory.MUTI_STR_VALUE);
            Dict itemDict = new Dict();
            itemDict.setPid(dict.getId());
            itemDict.setName(name);
            try {
                itemDict.setNum(Integer.valueOf(num));
            } catch (NumberFormatException e) {
                throw new GunsException(BizExceptionEnum.DICT_MUST_BE_NUMBER);
            }
            this.dictMapper.insert(itemDict);
        }
    }

    @Override
    public void editDict(Integer dictId, String dictName, String dicts) {
        //删除子字典
        Wrapper<Dict> dictEntityWrapper = new EntityWrapper<>();
        dictEntityWrapper = dictEntityWrapper.eq("pid", dictId);
        dictMapper.delete(dictEntityWrapper);
        //修改字典
        Dict dict = new Dict();
        dict.setId(dictId);
        dict.setName(dictName);
        dictMapper.updateById(dict);
        //添加子字典
        //解析dictValues
        List<Map<String, String>> items = MutiStrFactory.parseKeyValue(dicts);
        //添加字典条目
        for (Map<String, String> item : items) {
            String num = item.get(MutiStrFactory.MUTI_STR_KEY);
            String name = item.get(MutiStrFactory.MUTI_STR_VALUE);
            Dict itemDict = new Dict();
            itemDict.setPid(dict.getId());
            itemDict.setName(name);
            try {
                itemDict.setNum(Integer.valueOf(num));
            } catch (NumberFormatException e) {
                throw new GunsException(BizExceptionEnum.DICT_MUST_BE_NUMBER);
            }
            this.dictMapper.insert(itemDict);
        }
    }

    @Override
    public void delteDict(Integer dictId) {
        //删除这个字典的子词典
        Wrapper<Dict> dictEntityWrapper = new EntityWrapper<>();
        dictEntityWrapper = dictEntityWrapper.eq("pid", dictId);
        dictMapper.delete(dictEntityWrapper);

        //删除这个词典
        dictMapper.deleteById(dictId);
    }

    @Override
    public List<Dict> selectByCode(String code) {
        return this.baseMapper.selectByCode(code);
    }

    @Override
    public List<Map<String, Object>> list(String conditiion) {
        return this.baseMapper.list(conditiion);
    }

    @Override
    public List<Dict> queryChildList(Integer parentId) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("pid", parentId);
        wrapper.orderBy("num", true);
        return this.baseMapper.selectList(wrapper);
    }

    private List<Dict> queryPDList;
    private List<Dict> queryLMList;
    private List<Dict> queryBFLXList;
    private List<Dict> queryBJLXList;
    private List<Dict> queryGGLXList;
    private List<Dict> queryBCLXList;
    private List<Dict> queryGCSCList;
    private List<Dict> queryBFZQList;
    @Override
    public void clealCache(){
        queryPDList = null;
        queryLMList = null;
        queryBFLXList = null;
        queryBJLXList = null;
        queryGGLXList = null;
        queryBCLXList = null;
        queryGCSCList = null;
    }
    @Override
    public List<Dict> queryPDList() {
        if(queryPDList == null){
            queryPDList = queryChildList(45);
        }
        return queryPDList;
    }

    @Override
    public List<Dict> queryLMList() {
        if(queryLMList == null){
            queryLMList = queryChildList(51);
        }
        return queryLMList;
    }

    @Override
    public List<Dict> queryBFLXList() {
        if(queryBFLXList == null){
            queryBFLXList = queryChildList(58);
        }
        return queryBFLXList;
    }

    @Override
    public List<Dict> queryBJLXList() {
        if(queryBJLXList == null){
            queryBJLXList = queryChildList(61);
        }
        return queryBJLXList;
    }

    @Override
    public List<Dict> queryGGLXList() {
        if(queryGGLXList == null){
            queryGGLXList = queryChildList(64);
        }
        return queryGGLXList;
    }

    @Override
    public List<Dict> queryBCLXList() {
        if(queryBCLXList == null){
            queryBCLXList = queryChildList(72);
        }
        return queryBCLXList;
    }

    @Override
    public List<Dict> queryGCSCList() {
        if(queryGCSCList == null){
            queryGCSCList = queryChildList(80);
        }
        return queryGCSCList;
    }

    @Override
    public List<Dict> queryBFZQList() {
        if(queryBFZQList == null){
            queryBFZQList = queryChildList(138);
        }
        return queryBFZQList;
    }

    @Override
    public Result queryProductList() {
        clealCache();
        List<Dict> pdList = queryPDList();
        List<Dict> bclxList = queryBCLXList();
        List<Dict> bflxList = queryBFLXList();
        List<Dict> bjlxList = queryBJLXList();
        List<Dict> gcscList = queryGCSCList();
        List<Dict> gglxList = queryGGLXList();
        List<Dict> lmList = queryLMList();
        List<Dict> bfzqList = queryBFZQList();
        Map map = new HashMap();
        map.put("pdList", pdList);
        map.put("bclxList", bclxList);
        map.put("bflxList", bflxList);
        map.put("bjlxList", bjlxList);
        map.put("gcscList", gcscList);
        map.put("gglxList", gglxList);
        map.put("lmList", lmList);
        map.put("bfzqList", bfzqList);
        Result result = new Result();
        result.setData(map);
        return result;
    }

    @Override
    public Integer getChildMaxNum(Integer pid) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("pid", pid);
        wrapper.orderBy("num", false);
        wrapper.last("limit 1");
        Dict dict = this.selectOne(wrapper);
        if(dict != null){
            return dict.getNum();
        }
        return 0;
    }

    @Override
    public List<Dict> queryChildDict(Integer pid, String name) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("pid", pid);
        if(StringUtils.isNotBlank(name)){
            wrapper.like("name",name);
        }
        return this.selectList(wrapper);
    }
}
